//
//  CQNavgationController.m
//  01-OC微博
//
//  Created by 陈乾 on 15/10/27.
//  Copyright © 2015年 陈乾. All rights reserved.
//

#import "CQNavgationController.h"
#import "UIBarButtonItem+CQBarButtonItem.h"

@interface CQNavgationController ()<UINavigationControllerDelegate>

@property (nonatomic, strong) id popDelegate;

@end

@implementation CQNavgationController

+(void)initialize
{
    //拿到所有导航条上面的 UIBarButtonItem
    UIBarButtonItem *barButton = [UIBarButtonItem appearanceWhenContainedIn:self, nil];
    //富文本
    [barButton setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateNormal];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //先保存
    self.popDelegate = self.interactivePopGestureRecognizer.delegate;
#warning 就算是自己做自己的代理也要记得设置
    self.delegate = self;
    
    
}




#pragma mark - 代理方法 跳转完成的时候调用 显示谁就会吧谁的控制器传过来
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //表示显示根控制 。要还原手势滑动的代理还原
    if (viewController == self.viewControllers[0])
    {
        self.interactivePopGestureRecognizer.delegate = self.popDelegate;
    }else
    {
#warning  这样就算导航条上你自定义的按钮覆盖了系统的返回按钮 ，也能有手势返回 但这样做会出现重大bug 所以需要在返回根控制器的时候再把值还原----具体做法在上面
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    
}

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
  UITabBarController *tabBarVC = (UITabBarController*)[UIApplication sharedApplication].keyWindow.rootViewController;
    
    for (UIView *tabBarButton in tabBarVC.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarButton removeFromSuperview];
        }
    }
    

}


#pragma mark - 所有的跳转会调用这个方法
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //非根控制器 才需要设置导航栏左边返回上一个 右边返回跟控制器
    if(self.viewControllers.count != 0)
    {
#warning 这样只能隐藏系统自己的tabbar 所以你自定义的tabBar必须叫到系统的tabBar上面
        viewController.hidesBottomBarWhenPushed = YES;
        
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"navigationbar_back"] highImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] target:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        
        viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"navigationbar_more"] highImage:[UIImage imageNamed:@"navigationbar_more_highlighted"] target:self action:@selector(backToRoot) forControlEvents:UIControlEventTouchUpInside];
    }
    
#warning 记得调用父类的 帮你跳转
    [super pushViewController:viewController animated:animated];
}

#pragma mark - 导航条上的箭头回调方法
#warning 注意这里的self已经是导航控制器了 不需要在self.navigationController
- (void)back{
    [self popViewControllerAnimated:YES];
}

- (void)backToRoot{
    [self  popToRootViewControllerAnimated:YES];
}


@end
